﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using DLM.Domain.Model;
using DLM.Services.Interfaces;
using DLM.Web.ViewModel;
using DLM.Domain.DTO;
using DLM.Web.MVC.Extensions;
using DLM.Services.Implementations;
using System.Web.UI;

namespace DLM.Web.MVC.Controllers
{
    public class HomeController : Controller
    {
        private IPartyService partyService;
        private IUserService userService;
        
        public HomeController(IPartyService partyService,IUserService userService)
        {
            this.partyService = partyService;
            this.userService = userService;
        }

        public ActionResult Index(int? friendid, int? sportid, int? townid, int? otherid)
        {
            int pageIndex;
            int.TryParse(Request.QueryString["sportid"], out pageIndex);
            int townIndex;
            int.TryParse(Request.QueryString["townid"], out townIndex);
            int otherIndex;
            int.TryParse(Request.QueryString["otherid"], out otherIndex);
            IEnumerable<PartyDTO> friendList = this.partyService.ShowParty(PartyType.Friend,friendid ?? 0,10);
            IEnumerable<PartyDTO> homeList = this.partyService.ShowParty(PartyType.HomeTown, pageIndex, 10);
            IEnumerable<PartyDTO> sportList = this.partyService.ShowParty(PartyType.Sport, townIndex, 10);
            IEnumerable<PartyDTO> otherList = this.partyService.ShowParty(PartyType.Other, otherIndex, 10);

            HomePageView pageView = new HomePageView();
            pageView.Friend = friendList;
            pageView.HomeTown = homeList;
            pageView.Sport = sportList;
            pageView.Other = otherList;
            pageView.TopUser = PageDataService.TopBillBoard();
            if (Request.IsAjaxRequest())
            {
                if (friendid != null)
                {
                    return View("_Friend", pageView.Friend);
                }
                if (sportid != null)
                {
                    return View("_Sport", pageView.Sport);
                }
                if (townid != null)
                {
                    return View("_HomeTown", pageView.HomeTown);
                }
                if (otherid != null)
                {
                    return View("_Other", pageView.Other);
                }
            }
            return View(pageView);
        }

        [OutputCache(Duration = int.MaxValue, VaryByParam = "none")]
        public ActionResult About()
        {
            return View();
        }

        [UserAuthorize]
        public ActionResult Default(int? publishid, int? engagedid, int? interestid)
        {
            int pageIndex;
            int.TryParse(Request.QueryString["publishid"], out pageIndex);
            int interestIndex;
            int.TryParse(Request.QueryString["interestid"], out interestIndex);
            PartyPageView partyView = new PartyPageView();
            partyView.MyEngaged = this.partyService.MyEngaged(User.Identity.Name, engagedid ?? 0, 10);
            partyView.MyPublish = this.partyService.MyPublish(User.Identity.Name, pageIndex, 10);
            
            //int[] typeArray = partyView.MyEngaged.Select(p => p.PartyType).ToArray();
            //if (typeArray != null && typeArray.Count() > 0)
            //{
            //    partyView.MyInterest = this.partyService.MyInterest(typeArray, User.Identity.Name, interestIndex, 10);
            //}
            //else
            //{
            //    partyView.MyInterest = new List<PartyDTO>();
            //}
        
            //partyView.MyMessage = this.userService.GetMessage(User.Identity.Name,interestIndex,5);
            ViewData["MyMessage"] = this.userService.GetMessage(User.Identity.Name, interestIndex, 5);
            ViewData["Notice"] =this.userService.GetNotice(0, 2);
            if (Request.IsAjaxRequest())
            {
                if(publishid != null)
                {
                    return View("_MyPublished", partyView.MyPublish);
                }
                if (engagedid != null)
                {
                    return View("_MyEngaged",partyView.MyEngaged);
                }
                if (interestid != null)
                {
                    return View("_MyInteresting", partyView.MyInterest);
                }
            }
            return View(partyView);
        }

        [HttpGet]
        [UserAuthorize]
        public ActionResult MyEngaged(int? page)
        {
            var list = this.partyService.MyEngaged(User.Identity.Name, page ?? 1, 2);
            return View(list);
        }

        [OutputCache(Duration =int.MaxValue, VaryByParam="none")]
        public ActionResult Footer()
        {
            return PartialView("_Footer");
        }
 
    }
}
